﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MiniBug.Controllers
{
    public class ProjectController : Controller
    {

        private Entity.MSSQLEntity dbContext = new Entity.MSSQLEntity();
        //
        // GET: /Project/

        public ActionResult Index()
        {
            return View(dbContext.Project.ToList());
        }

        //
        // GET: /Project/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude="idProject")] Entity.Project newProject)
        {
            try
            {
                dbContext.AddToProject(newProject);
                dbContext.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Project/Edit/5
 
        public ActionResult Edit(int id)
        {
            var result = (from p in dbContext.Project
                           where p.idProject == id
                          select p).FirstOrDefault();
            return View(result);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        [Authorize]
        public ActionResult Edit(Entity.Project projectToEdit)
        {
            try
            {
               var originalProject = (from p in dbContext.Project
                                      where p.idProject == projectToEdit.idProject
                                      select p).FirstOrDefault();
                
                dbContext.ApplyCurrentValues(originalProject.EntityKey.EntitySetName,
                    projectToEdit);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Project/Delete/5
 
        public ActionResult Delete(int id)
        {
            var projectToDelete = (from p in dbContext.Project
                                   where p.idProject == id
                                   select p).FirstOrDefault();
            dbContext.DeleteObject(projectToDelete);
            dbContext.SaveChanges();
            return RedirectToAction("Index");
          
        }

        //
        // POST: /Project/Delete/5

        [HttpPost]
        public ActionResult Delete(Entity.Project projectToDelete)
        {
            try
            {
                var originalProject = (from p in dbContext.Project
                                       where p.idProject == projectToDelete.idProject
                                       select p).FirstOrDefault();
                dbContext.DeleteObject(originalProject);
                dbContext.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
